"""
数据处理模块模型
定义处理后数据存储和处理任务相关的模型
"""

from django.db import models
from django.contrib.auth.models import User
from apps.common.models import BaseModel, TaskStatus, DataStatus
from apps.crawler.models import RawData


class ProcessedData(BaseModel):
    """
    处理后数据模型
    存储经过清洗和AI处理后的结构化数据
    """
    raw_data = models.OneToOneField(
        RawData,
        on_delete=models.CASCADE,
        verbose_name="原始数据",
        help_text="关联的原始数据记录"
    )
    title = models.CharField(
        max_length=500,
        verbose_name="标题",
        help_text="公告或项目标题"
    )
    content = models.TextField(
        verbose_name="内容",
        help_text="清洗后的文本内容"
    )
    category = models.CharField(
        max_length=100,
        blank=True,
        verbose_name="分类",
        help_text="数据分类，如招标、中标、采购等"
    )
    project_number = models.CharField(
        max_length=100,
        blank=True,
        verbose_name="项目编号",
        help_text="项目或公告的编号"
    )
    amount = models.DecimalField(
        max_digits=15,
        decimal_places=2,
        null=True,
        blank=True,
        verbose_name="金额",
        help_text="项目金额或预算"
    )
    currency = models.CharField(
        max_length=10,
        default="CNY",
        verbose_name="货币单位",
        help_text="金额的货币单位"
    )
    publish_date = models.DateField(
        null=True,
        blank=True,
        verbose_name="发布日期",
        help_text="公告发布日期"
    )
    deadline = models.DateField(
        null=True,
        blank=True,
        verbose_name="截止日期",
        help_text="投标或报名截止日期"
    )
    organization = models.CharField(
        max_length=200,
        blank=True,
        verbose_name="发布机构",
        help_text="发布公告的机构或单位"
    )
    contact_info = models.JSONField(
        default=dict,
        verbose_name="联系信息",
        help_text="联系人、电话、邮箱等信息"
    )
    location = models.CharField(
        max_length=200,
        blank=True,
        verbose_name="地区",
        help_text="项目所在地区"
    )
    tags = models.JSONField(
        default=list,
        verbose_name="标签",
        help_text="数据标签，用于分类和检索"
    )
    ai_processed_content = models.JSONField(
        default=dict,
        verbose_name="AI处理结果",
        help_text="AI模型处理后的结构化信息"
    )
    confidence_score = models.FloatField(
        default=0.0,
        verbose_name="置信度",
        help_text="AI处理结果的置信度分数"
    )
    status = models.CharField(
        max_length=20,
        choices=DataStatus.choices,
        default=DataStatus.PROCESSED,
        verbose_name="数据状态",
        help_text="数据的处理状态"
    )
    quality_score = models.FloatField(
        default=0.0,
        verbose_name="质量评分",
        help_text="数据质量评分"
    )

    class Meta:
        verbose_name = "处理后数据"
        verbose_name_plural = "处理后数据"
        db_table = "processor_processed_data"
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['category', 'status']),
            models.Index(fields=['publish_date']),
            models.Index(fields=['amount']),
            models.Index(fields=['organization']),
        ]

    def __str__(self):
        return f"{self.category} - {self.title[:50]}"


class ProcessTask(BaseModel):
    """
    数据处理任务模型
    记录数据处理任务的执行情况
    """
    task_name = models.CharField(
        max_length=200,
        verbose_name="任务名称",
        help_text="处理任务的名称"
    )
    task_type = models.CharField(
        max_length=50,
        verbose_name="任务类型",
        help_text="处理任务的类型，如清洗、AI处理等"
    )
    status = models.CharField(
        max_length=20,
        choices=TaskStatus.choices,
        default=TaskStatus.PENDING,
        verbose_name="任务状态",
        help_text="当前任务的执行状态"
    )
    start_time = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name="开始时间",
        help_text="任务开始执行的时间"
    )
    end_time = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name="结束时间",
        help_text="任务结束的时间"
    )
    total_records = models.IntegerField(
        default=0,
        verbose_name="总记录数",
        help_text="需要处理的总记录数"
    )
    processed_records = models.IntegerField(
        default=0,
        verbose_name="已处理记录数",
        help_text="已经处理完成的记录数"
    )
    success_count = models.IntegerField(
        default=0,
        verbose_name="成功数量",
        help_text="成功处理的记录数"
    )
    error_count = models.IntegerField(
        default=0,
        verbose_name="错误数量",
        help_text="处理失败的记录数"
    )
    error_message = models.TextField(
        blank=True,
        verbose_name="错误信息",
        help_text="任务执行过程中的错误信息"
    )
    config = models.JSONField(
        default=dict,
        verbose_name="任务配置",
        help_text="任务的具体配置参数"
    )
    result_summary = models.JSONField(
        default=dict,
        verbose_name="结果摘要",
        help_text="任务执行结果的统计摘要"
    )
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="创建者",
        help_text="创建该任务的用户"
    )

    class Meta:
        verbose_name = "处理任务"
        verbose_name_plural = "处理任务"
        db_table = "processor_task"
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.task_type} - {self.task_name}"

    @property
    def progress_percentage(self):
        """计算任务进度百分比"""
        if self.total_records == 0:
            return 0
        return round((self.processed_records / self.total_records) * 100, 2)


class DataValidation(BaseModel):
    """
    数据验证模型
    记录数据验证的结果和问题
    """
    processed_data = models.ForeignKey(
        ProcessedData,
        on_delete=models.CASCADE,
        verbose_name="处理后数据",
        help_text="关联的处理后数据"
    )
    validation_type = models.CharField(
        max_length=50,
        verbose_name="验证类型",
        help_text="验证的类型，如格式验证、业务验证等"
    )
    is_valid = models.BooleanField(
        verbose_name="是否有效",
        help_text="验证是否通过"
    )
    validation_score = models.FloatField(
        default=0.0,
        verbose_name="验证分数",
        help_text="验证的分数或置信度"
    )
    issues = models.JSONField(
        default=list,
        verbose_name="问题列表",
        help_text="验证发现的问题列表"
    )
    suggestions = models.JSONField(
        default=list,
        verbose_name="建议列表",
        help_text="改进建议列表"
    )
    validator = models.CharField(
        max_length=100,
        verbose_name="验证器",
        help_text="执行验证的验证器名称"
    )

    class Meta:
        verbose_name = "数据验证"
        verbose_name_plural = "数据验证"
        db_table = "processor_validation"
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.validation_type} - {'通过' if self.is_valid else '未通过'}"
